<template>
  <el-dialog :title="isadd?'添加菜单':'编辑菜单'" :visible.sync="isshow" :before-close="cancel" center>
    <el-form :model="menuinfo" :rules="rules" ref="dialogform">
      <el-form-item label="菜单名称" label-width="120px" prop="title">
        <el-input v-model="menuinfo.title"></el-input>
      </el-form-item>
      <el-form-item label="上级菜单" label-width="120px" prop="pid">
        <el-select v-model="menuinfo.pid" placeholder="请选择上级菜单" @change="changeType">
          <el-option label="顶级菜单" :value="0"></el-option>
          <el-option
            v-for="item in get_menuList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜单类型" label-width="120px">
        <el-radio v-model="menuinfo.type" :label="1" disabled>目录</el-radio>
        <el-radio v-model="menuinfo.type" :label="2" disabled>菜单</el-radio>
      </el-form-item>
      <el-form-item label="菜单地址" label-width="120px" v-if="menuinfo.type==1">
        <el-select v-model="menuinfo.url" placeholder="请选择">
          <el-option
            v-for="item in pathArr"
            :key="item"
            :label="item.meta.title"
            :value="'/index/' + item.path"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜单图标" label-width="120px" v-else>
        <el-select v-model="menuinfo.icon" placeholder="请选择">
          <el-option v-for="item in iconArr" :key="item.id" :label="item.icon" :value="item.icon">
            <i :class="item.icon"></i>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" label-width="120px">
        <el-switch
          v-model="menuinfo.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="addItem" v-if="isadd">添 加</el-button>
      <el-button type="primary" @click="editItem" v-else>编 辑</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { menuadd, menuinfo, menuedit } from "@/request/api.js";
import { mapGetters, mapActions } from "vuex";
import { pathArr } from "@/router/index.js";
export default {
  props: ["isshow", "isadd"],
  data() {
    return {
      //表单绑定数据--和后端要求的字段保持一致
      menuinfo: {
        pid: "", //上级分类编号  注意：顶级菜单-0
        title: "", //菜单名称
        type: 2, //类型 1目录2菜单
        icon: "", //图标 string
        url: "", //菜单地址 string
        status: 1 //状态   1正常2禁用   number类型
      },
      //定义表单规则
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }]
      },
      //定义下拉列表的图标选择数据
      iconArr: [
        { id: 1, icon: "el-icon-s-tools" },
        { id: 2, icon: "el-icon-s-goods" },
        { id: 3, icon: "el-icon-picture" },
        { id: 4, icon: "el-icon-video-camera-solid" },
        { id: 5, icon: "el-icon-s-custom" }
      ],
      //定义下拉列表的菜单地址选择
      pathArr
    };
  },
  methods: {
    ...mapActions({
      action_menuList: "menu/action_menuList"
    }),
    //1.取消
    cancel() {
      //子传父通过自定义属性  - 发通知告诉父组件该改数据
      this.$emit("cancel");
      //重置
      this.reset();
    },
    //2.添加一项
    addItem() {
      /* 1.点击添加（换了一个数据=无数据的*/
      //2.表单能通过验证才可以添加到数据库
      this.$refs.dialogform.validate(async val => {
        if (!val) {
          //false
          return;
        }

        //3.发起ajax请求，将数据添加到数据库中
        try {
          let res = await menuadd(this.menuinfo);
          if (res.code == 200) {
            //添加成功--弹框
            this.$message.success(res.msg);
            //关闭弹窗组件
            this.cancel();
            //重新从后端拿数据
            this.action_menuList();
          } else {
            this.$message.warning(res.msg);
          }
        } catch (err) {
          this.$message.error(err);
        }
      });
    },
    //3.编辑一项
    editItem() {
      /* 1.点击添加（换了一个数据=无数据的*/
      //2.表单能通过验证才可以添加到数据库
      this.$refs.dialogform.validate(async val => {
        if (!val) {
          //false
          return;
        }

        //3.发起ajax请求，将数据添加到数据库中
        try {
          let res = await menuedit(this.menuinfo);
          if (res.code == 200) {
            //添加成功--弹框
            this.$message.success(res.msg);
            //关闭弹窗组件
            this.cancel();
            //重新从后端拿数据
            this.action_menuList();
          } else {
            this.$message.warning(res.msg);
          }
        } catch (err) {
          this.$message.error(err);
        }
      });
    },
    //4.重置
    reset() {
      //重置数据
      this.menuinfo = {
        pid: "", //上级分类编号  注意：顶级菜单-0
        title: "", //菜单名称
        type: 1, //类型 1目录2菜单
        icon: "", //图标 string
        url: "", //菜单地址 string
        status: 1 //状态   1正常2禁用   number类型
      };

      //清空表单验证 - form  ref:定义的时候不能重名
      this.$refs.dialogform.resetFields();
    },

    //5.数据回显 - 弹框一出来，请求一条数据
    async lookup(id) {
      let res = await menuinfo({ id });
      if (res.code == 200) {
        this.menuinfo = res.list;
        //添加一个id属性
        this.menuinfo.id = id;
      }
    },

    //6.选择菜单发生变化时触发
    changeType() {
      if (this.menuinfo.pid == 0) {
        this.menuinfo.type = 2;
      } else {
        this.menuinfo.type = 1;
      }
    }
  },
  computed: {
    ...mapGetters({
      get_menuList: "menu/get_menuList"
    })
  }
};
</script>

<style>
</style>